<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/rp/common/taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>">
    
    <title>数据导入</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	
	<script src="rp/scripts/boot.js" type="text/javascript"></script>
	
	<style type="text/css">
	   	
    </style>
	
  </head>
  
  <body class="body-p5">
  	
  	<form id="form1" method="post">
  		<fieldset>
  			<legend>友情提示</legend>
  			<span style="color: red;">数据导入功能目前仍存在导入多次之后可能报内存溢出（java.lang.OutOfMemoryError: PermGen space）的问题，请尽量一次性导入需要导入的数据，尽可能的少执行导入操作，后续会跟进修复，还望见谅！</span>
  		</fieldset>
  		<fieldset>
  			<legend>数据来源配置</legend>
	  		<table>
	  			<tr>
	  				<td>数据库：</td>
	  				<td>
	  					<input 
	  						id="dbType" name="dbType" valueField="dbType" class="mini-combobox" value="oracle" allowInput="false" required="true"
	  						data="[{dbType: 'oracle', text: 'ORACLE'}, {dbType: 'mssql', text: 'MSSQL'}, {dbType: 'mysql', text: 'MYSQL'}]"/>
	  				</td>
	  				<td>URL:</td>
	  				<td><input id="url" name="url" class="mini-textbox" required="true" style="width: 300px;"/></td>
	  				<td>登录名：</td>
	  				<td><input id="username" name="username" class="mini-textbox" required="true" /></td>
	  				<td>密码：</td>
	  				<td><input id="password" name="password" class="mini-password" required="true" /></td>
	  				<td>
	  					<a href="javascript:;" onclick="testLink();">测试连接</a>
	  					<!-- <a href="javascript:;" onclick="">保存配置</a> -->
	  				</td>
	  			</tr>
	  		</table>
  		</fieldset>
  		<fieldset>
  			<legend>数据表</legend>
  			<table>
  				<input id="targetChannelId" type="hidden"/>
  				<!-- <tr>
  					<td><a href="javascript:;">全选</a> | <a href="javascript:;">清空选择</a></td>
  				</tr> -->
  				<tr>
  					<td>
  						<div name="menuAbout" class="mini-checkbox" checked="false" readOnly="false" text="菜单与菜单项"></div>
  						<!-- <div id="ck2" name="infoChannel" class="mini-checkbox" checked="false" readOnly="false" text="信息栏目" onvaluechanged="onValueChanged"></div>
  						<div id="ck3" name="infoArticle" class="mini-checkbox" checked="false" readOnly="false" text="信息文章" onvaluechanged="onValueChanged"></div> -->
  						<br/>
  						<div name="systemRole" class="mini-checkbox" checked="false" readOnly="false" text="系统角色"></div>
  						<br/>
  						<div name="systemModule" class="mini-checkbox" checked="false" readOnly="false" text="系统模块"></div>
  						<div name="systemAuthority" class="mini-checkbox" checked="false" readOnly="false" text="系统权限"></div>
  						<div name="systemResource" class="mini-checkbox" checked="false" readOnly="false" text="系统资源"></div>
  						<br/>
  						<h4>信息发布模块</h4>
  						<div name="infoChannel" class="mini-checkbox" checked="false" readOnly="false" text="信息栏目"></div>
  						<div id="infoArticle" name="infoArticle" class="mini-checkbox" 
  							checked="false" readOnly="false" text="信息文章"
  							onvaluechanged="onInfoArticleValueChanged"></div>&nbsp;(目标栏目：<span id="targetChannelText">未选</span>)
  						
  						
  					</td>
  				</tr>
  			</table>	
  		</fieldset>
  		<table>
  			<tr>
  				<td><a class="mini-button" iconCls="icon-dataImport" onclick="submitForm()">开始导入</a></td>
  			</tr>
  		</table>
  	</form>
  	
	<script type="text/javascript">
		$(function(){
			
		});
		
		function testLink(){
			var form = new mini.Form("#form1");
			form.validate();
            if (form.isValid() == false) return;
			
			var dbType = mini.get("dbType").getValue();
			var url = mini.get("url").getValue();
			var username = mini.get("username").getValue();
			var password = mini.get("password").getValue();
			rp_ajax("rp/system/baseData/testDsLink",{dbType:dbType,url:url,username:username,password:password},function(text){
				if(text == "success"){
					mini.alert("连接成功");
				}else{
					mini.alert("无法连接数据源，请再检查您的配置");
				}
			});
		}
		
		function onInfoArticleValueChanged(e){
			var checked = this.getChecked();
			if(checked){
				
				var form = new mini.Form("#form1");
				form.validate();
	            if (form.isValid() == false){
	            	this.setChecked(false);
	            	mini.alert("如需导入信息文章，必须先填写数据来源配置");
	            	return;
	            }
				
				mini.open({
	                url: "rp/system/baseData/selectTargetChannel.jsp",
	                title: "选择目标栏目", 
	                width: 200, 
	                height: 300,
	                onload: function () {
	                	var dbType = mini.get("dbType").getValue();
	        			var url = mini.get("url").getValue();
	        			var username = mini.get("username").getValue();
	        			var password = mini.get("password").getValue();
	        			
	                	var iframe = this.getIFrameEl();
                		iframe.contentWindow.initDc(dbType,url,username,password);
	                },
	                ondestroy: function (action) {
	                	if (action == "ok") {
	                        var iframe = this.getIFrameEl();
							var node = iframe.contentWindow.getSelectData();
							$("#targetChannelId").val(node.id);
							$("#targetChannelText").text(node.name);
	                    }else{
	                    	var infoArticle = mini.get("infoArticle");
	                    	infoArticle.setChecked(false);
	                    	$("#targetChannelId").val("");
	                    }
	                }
	            });
			}else{
				$("#targetChannelText").text("未选");
			}
		}
		
		function submitForm(){
			var form = new mini.Form("#form1");
			
			form.validate();
            if (form.isValid() == false) return;
            
            
            
          	//提交表单数据            
            var data = form.getData();      //获取表单多个控件的数据
            var json = mini.encode(data);   //序列化成JSON
            //mini.alert(json);
            rp_ajax("rp/system/baseData/dataImport", {json: json, targetChannelId: $("#targetChannelId").val()}, function(text){
            	if(text == "success"){
            		mini.alert("导入成功");
            	}else{
            		mini.alert("数据导入过程中出错");
            	}
            });
		}
	</script>
  </body>
</html>
